package com.example.warehouse.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.warehouse.bus.dao.persist.mapper.BusBreakageMapper;
import com.example.warehouse.bus.pojo.entity.BusBreakage;
import com.example.warehouse.bus.pojo.entity.BusRepertory;
import com.example.warehouse.bus.pojo.param.BusBreakRequestParam;
import com.example.warehouse.bus.pojo.param.BusBreakageQueryParam;
import com.example.warehouse.bus.pojo.param.BusRepertoryAddGoodsNum;
import com.example.warehouse.bus.pojo.param.BusRepertoryReduceGoodsNumParam;
import com.example.warehouse.bus.service.IBusBreakageService;
import com.example.warehouse.bus.service.IBusRepertoryService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.example.warehouse.common.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class IBusBreakageServiceImpl implements IBusBreakageService {
    @Autowired
    private BusBreakageMapper bbm;
    @Autowired
    private IBusRepertoryService ibrs;
    @Override
    public PageData<BusBreakage> pageHelperByPageNum(Integer pageNum) {
        Integer pageSize=10;
        return pageHelperByPageNumAndPageSize(pageNum,pageSize);
    }

    @Override
    public PageData<BusBreakage> pageHelperByPageNumAndPageSize(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BusBreakage> list = bbm.list();
        PageInfo<BusBreakage> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    /**
     * 处理报损的业务逻辑
     * @param busBreakRequestParam
     * 对库存进行一个减库存的操作，将报损的参数添加到数据库报损表中
     */
    @Override
    public void breakageRequest(BusBreakRequestParam busBreakRequestParam) {
        //通过商品名查出库存商品信息,得到商品供应商及其产地
        BusRepertory repertoryData = bbm.findByGoodsNameGoodsRepertory(busBreakRequestParam.getGoodsName());
        if(repertoryData==null)
        {
            String message="请输入正确的商品名称";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        //将报损信息插入报损单中
        BusBreakage busBreakage=new BusBreakage();
        BeanUtils.copyProperties(busBreakRequestParam,busBreakage);
        busBreakage.setProviderName(repertoryData.getProviderName());
        busBreakage.setProducePlace(repertoryData.getProducePlace());
        busBreakage.setGmtCreate(LocalDateTime.now());
        bbm.insert(busBreakage);
        //现执行减库存的操作
        BusRepertoryReduceGoodsNumParam busRepertoryReduceGoodsNumParam=new BusRepertoryReduceGoodsNumParam()
                .setGoodsName(busBreakRequestParam.getGoodsName())
                .setGoodsNum(busBreakRequestParam.getBreakageNum());
        ibrs.reduceGoodsNum(busRepertoryReduceGoodsNumParam);
    }

    /**
     * 根据选择的查询的类型和关键字进行的模糊查询并且进行分页然后返回给前端
     * @param busBreakageQueryParam
     * @return
     */
    @Override
    public PageData<BusBreakage> findByKeywordAndTypeFuzzyQuery(BusBreakageQueryParam busBreakageQueryParam,Integer pageNum) {
        Integer pageSize=10;
        PageHelper.startPage(pageNum,pageSize);
        List<BusBreakage> list = bbm.findByKeywordAndTypeFuzzyQuery(busBreakageQueryParam);

        PageInfo<BusBreakage> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
